package com.stox.dto;

import java.io.Serializable;

import lombok.Data;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.springframework.util.Assert;

import com.stox.dto.intf.Identifiable;
import com.stox.dto.intf.Nameable;

@Data
@JsonIgnoreProperties(ignoreUnknown=true)
public abstract class AbstractModel implements Serializable, Comparable<Nameable>, Identifiable, Nameable{
	private static final long serialVersionUID = 1L;

	public static class Type implements Serializable{
		private static final long serialVersionUID = 1L;

		public static final Type ANY = new Type("");
		
		private final String path;
		
		protected Type(String path) {
			Assert.notNull(path);
			this.path = path;
		}
		
		public String getPath() {
			return path;
		}

		public boolean isSubTypeOf(Type type){
			return null == type ? false : path.startsWith(type.path);
		}
		
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((path == null) ? 0 : path.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Type other = (Type) obj;
			if (path == null) {
				if (other.path != null)
					return false;
			} else if (!path.trim().equalsIgnoreCase(other.path.trim()))
				return false;
			return true;
		}
	}
	
	private Long id;
	
	private String name;
	
	private final Type type;
	
	public AbstractModel(Type type) {
		this.type = type;
	}
	
	@Override
	public int compareTo(Nameable o) {
		if(null != o && null != o.getName() && null != getName())
			return getName().compareToIgnoreCase(o.getName());
		return 0;
	}
	
	public Type getType(){
		return type;
	}
	
	public String toString(){
		return getName();
	}
	
}
